package com.tjetc.dao.impl;

import com.tjetc.dao.HotelDao;
import com.tjetc.pojo.dto.CheckInDTO;
import com.tjetc.pojo.entity.Hotel;
import com.tjetc.utils.DruidUtils;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.List;


/**
 * @Author: itda
 * @Date: 2024/5/10 16:29
 * @Description: HotelDaoImpl
 */
public class HotelDaoImpl implements HotelDao {

    @Override
    public void save(Hotel hotel) throws Exception {
        DruidUtils.r1.update(DruidUtils.getConnection(), "insert into hotel (name, address, city, description, phone) values (?,?,?,?,?)"
                , hotel.getName(), hotel.getAddress(), hotel.getCity(), hotel.getDescription(), hotel.getPhone());
    }

    @Override
    public void deleteById(Long hotelId) throws Exception {
        DruidUtils.r1.update(DruidUtils.getConnection(), "delete from hotel where id = ?", hotelId);
    }

    @Override
    public void update(Hotel hotel) throws Exception {
        DruidUtils.r1.update(DruidUtils.getConnection(), "update hotel set name = ?,address = ?,city = ?,description = ?,phone = ? where id = ?"
                , hotel.getName(), hotel.getAddress(), hotel.getCity(), hotel.getDescription(), hotel.getPhone(), hotel.getId());
    }

    @Override
    public List<Hotel> list() throws Exception {
        return DruidUtils.r2.query("select * from hotel", new BeanListHandler<>(Hotel.class));
    }

    @Override
    public Hotel getById(Long hotelId) throws Exception {
        return DruidUtils.r2.query("select * from hotel where id = ?", new BeanHandler<>(Hotel.class), hotelId);
    }

    @Override
    public void updateRating(Long hotelId, double rating) throws Exception {
        DruidUtils.r1.update(DruidUtils.getConnection(), "update hotel set rating = ? where id = ?", rating, hotelId);
    }

}
